package net.url;

import java.io.IOException;
import java.net.CacheRequest;
import java.net.CacheResponse;
import java.net.ResponseCache;
import java.net.URI;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MemoryCache extends ResponseCache
{
	private Map<URI,SimpleCacheResponse> responses = new ConcurrentHashMap<URI, SimpleCacheResponse>();
	private int maxEntries = 100;
	public MemoryCache()
	{
		this(100);
	}
	public MemoryCache(int maxEntries)
	{
		this.maxEntries = maxEntries;
	}
	@Override
	public CacheResponse get(URI uri, String rqstMethod, Map<String, List<String>> rqstHeaders) throws IOException
	{
		SimpleCacheResponse response = responses.get(uri);
		return response;
	}

	@Override
	public CacheRequest put(URI uri, URLConnection conn) throws IOException
	{
		if(responses.size()>maxEntries)
		{
			return null;
		}
		String cacheControl = conn.getHeaderField("Cache-Control");
		System.out.println(cacheControl);
		SimpleCacheRequest request = new SimpleCacheRequest();
		SimpleCacheResponse response = new SimpleCacheResponse(request, conn);
		responses.put(uri, response);
		return null;
	}
	
}
